<?php
	/**
	 *	Copyright (c) TeamFlamingo And gray 2004-2009
	 *	www.me-live.com.cn / www.i-gray.cn
	 *
	 *	[ WARNING ]
	 *		This is NOT a freeware!
	 *		You MUST get author's authorization before using it!
	 *	[ WARNING ]
	 *
	 *	Name : index.php / Development Code Evolve
	 *	Created / Modify : 2010-2-15 / --
	 */

	include	'./init.php';

	if( !extension_loaded( 'memcache' ) )
	{
		$prefix = PHP_SHLIB_SUFFIX == 'dll' ? 'php_' : '';
		if( function_exists( 'dl' ) && @dl( "{$prefix}memcache." . PHP_SHLIB_SUFFIX ) )
			define( 'MEMCACHED_MODULE_LOADED', true );
		else
			define( 'MEMCACHED_MODULE_LOADED', false );
	}
	else
	{
		define( 'MEMCACHED_MODULE_LOADED', true );
	}

	//	[CH]	初始化变量
	$action	=	$MDK	=	array();
	$Request	=	$archiveFile	=	null;
	$currentForumUserResult	=	array();
	$currentForumUserInfo	=	false;

	//	[CH]	设置Memcached缓存
	include	ME_CORE . '/memcached.lib.php';
	include	ME_CORE . '/database.lib.php';
	include	ME_CORE . '/tree.lib.php';
	include	ME_CORE . '/common.h.php';

	//	[CH]	判断用户是否已经登录
	isUserLogin();

	//	[CH]	把GET数据分解到MDK数组
	/**
	 *	这里把当前 URL ? 后面的字符分解到 $MDK数组
	 *	关键值 $MDK['do'] 是用来判断访问的是哪个模块
	 *	do 的值被定义在下面的 $action 数组里
	 *	如果用户输入的 do=.... 值不在数组里
	 *	就直接跳转至首页
	 */
	parse_str( $_SERVER['QUERY_STRING'], $MDK );
	$MDK['p']	=	isset( $MDK['p'] ) && !empty( $MDK['p'] ) ? intval( $MDK['p'] ) : 1;
	array_map( 'trim', $MDK );

	//	[CH]	这里需要进行判断, 检测一下是否有后台改写
	$CookiePath	=	ME_COOKIE_PATH;
	$CookieDomain	=	$HostName;

	//	[CH]	定义可调用用的模块
	/**
	 *	这里定义几个值
	 *	forum	是真正意义上的首页
	 *	view	是浏览文章的页面
	 */
	$action		=	array( 'forum', 'view', 'write' );
	//	[CH]	设定当前调用的模块
	$MDK['do']	=	isset( $MDK['do'] ) && !empty( $MDK['do'] ) && in_array( $MDK['do'], $action ) ? $MDK['do'] : 'forum';

	//	[CH]	启用Memcached缓存
	//$MC	=	new	MDKmemcached( $options );

	include	ME_CORE . '/sys.inc.php';
	include	ME_FORUM_ADMIN_CACHE_FILE;
	include ME_FORUM_CACHE_FILE;

	$tree	=	new Tree( $forumsResult );
	$forumsMenu	=	$tree->parse();
	//print_r( $forumsResult );
	//$tree->parse();

	/**
	 *	这里判断一下是不是首页也就是没有进入版块
	 *	$MDK['f']	这个值没有设置
	 *	并且 $MDK['do'] == 'forum' 也就是调用首页显示模块
	 *
	 *	如果是首页就显示欢迎新会员
	 *	否则就显示 斑竹 信息
	 */

	//	[CH]	根据请求设定存档页面
	switch ( $MDK['do'] )
	{
		case	'forum'	:
			$forumName	=	getForumName();
			if( isset( $MDK['f'] ) && !empty( $MDK['f'] ) )
			{
				$MDK['f']	=	getMeInt( $MDK['f'] );
				$Request	=	"/forum-{$MDK['f']}-{$MDK['p']}.html";

				//	[CH]	这里把当前版块的斑竹信息存入了 $forumAdminForumArr 数组
				//	[CH]	见 ./kernel/common.h.php
				/*$tempArr	=	$forumAdminForumArr	=	array();
				foreach( $forumAdmin as $tempArr )
				{
					if( $tempArr['fid'] == $MDK['f'] )
						$forumAdminForumArr[]	=	$tempArr;
				}*/

				$currentForumUserInfo	=	getForumAdminInfo( $forumAdmin, $MDK['f'] );
			}
			else
			{
				$Request	=	"/forum-{$MDK['p']}.html";
				$currentForumUserResult	=	$db->assoc( $db->query( "SELECT `m`.`author`, `m`.`uid` FROM `mdk_users` AS `u`, `mdk_member` AS `m` WHERE `u`.`uid` = `m`.`uid` ORDER BY `u`.`uid` DESC LIMIT 0, 1" ) );
				$currentForumUserInfo	=	"<span id=\"member\">欢迎新会员</span> <a href=\"./?do=user&u={$currentForumUserResult['uid']}\">{$currentForumUserResult['author']}</a> 的到来";
				/**
				 *	预留值 $MDK['do'] == 'user'
				 *	用来查看 用户信息的模块
				 *	---------------------------------------------
				 *	这里尽量使用了短变量名
				 *	'f' / $MDK['f']	表示	版块ID	ForumID
				 *	'u' / $MDK['u']	表示	用户ID	UID
				 *	't' / $MDK['t']	表示	文章ID	TID
				 *	'p' / $MDK['p']	表示	页码
				 */
			}
		break;

		case	'view'	:
			if( isset( $MDK['t'] ) && !empty( $MDK['t'] ) )
			{
				include	ME_CORE . '/updata.h.php';
				updataReadData( $MDK['t'] );
				$MDK['t']	=	getMeInt( $MDK['t'] );
				$Request	=	"/thread-{$MDK['t']}-{$MDK['p']}.html";
			}
			else
			{
				header( "Location: ./" );
				exit;
			}
		break;

		case	'write'	:
			if( isset( $MDK['f'] ) && !empty( $MDK['f'] ) )
				$MDK['f']	=	getMeInt( $MDK['f'] );
			else
				include	ME_SYSTEM . '/error.php';
		break;

		default	:
			include	ME_SYSTEM . '/error.php';
			//echo	'';
			//header( "Location: " . ME_SYSTEM . '/error.php' );
		//exit;
	}

	$archiveFile	=	$Request ? ME_ARCHIVE_DIR . $Request : false;

	//	[CH]	测试Memcached,暂时关闭
	if( $archiveFile && file_exists( $archiveFile ) && ( time() - filemtime( $archiveFile ) ) < ME_ARCHIVE_LIFE )
	{
		$appRunTime	=	round( getCurrentTime() - $appStartTime, 4 );
		include	$archiveFile;
		exit;
	}

	include	ME_SYSTEM . "/{$MDK['do']}.php";
	include	template( $MDK['do'] );

	if( !ME_DB_PCONNECT )
		$db	=	null;

	if( !is_dir( ME_ARCHIVE_DIR ) )
	{
		@mkdir( ME_ARCHIVE_DIR, 0777 );
	}

	//	[CH]	测试Memcached,暂时关闭
	//if( $archiveFile )
	//	createNewArchiveFile( $archiveFile, ob_get_contents() );
	ob_end_flush();
?>